package com.estore.managerImpl;

import java.util.ArrayList;
import java.util.List;

import com.estore.converter.BlankRemover;
import com.estore.dao.Product_Dao;
import com.estore.dao.ShopDao;
import com.estore.entity.Brand;
import com.estore.entity.Categories;
import com.estore.entity.FavoriteProduct;
import com.estore.entity.MainCategories;
import com.estore.entity.Product;
import com.estore.entity.ProductList;
import com.estore.entity.Product_Image;
import com.estore.info.AgentProductInfo;
import com.estore.info.BrandInfo;
import com.estore.info.ProductImageInfo;
import com.estore.info.ProductInfo;
import com.estore.info.SubCatInfo;
import com.estore.manager.ProductManager;

public class ProductManagerImpl implements ProductManager {

	private Product_Dao productDao;
	private ShopDao shopDao;

	public ShopDao getShopDao() {
		return shopDao;
	}

	public void setShopDao(ShopDao shopDao) {
		this.shopDao = shopDao;
	}

	@Override
	public List<Categories> listCategories() {
		List<MainCategories> categories = productDao.listMainCategories();
		List<Categories> list = new ArrayList<Categories>();

		if (categories != null) {
			for (MainCategories record : categories) {
				list.add(new Categories(record.getMainCatID(), record.getMainCatName()));
			}
		}
		return list;
	}

	@Override
	public List<SubCatInfo> listSubCatInfo() {
		List<MainCategories> listMainCategories = productDao.listMainCategories();
		List<SubCatInfo> subList = new ArrayList<SubCatInfo>();

		if (listMainCategories != null) {
			for (MainCategories record : listMainCategories) {
				List<Categories> subCategories = productDao
						.listSubCategories(record.getMainCatID());
				SubCatInfo subCatInfo = new SubCatInfo(record.getMainCatID(), record.getMainCatName()
						, subCategories);
				subList.add(subCatInfo);
			}
		}
		return subList;
	}

	@Override
	public List<ProductInfo> getListProduct(int catID) {
		List<Product> listproduct = productDao.getListProduct(catID);
		List<ProductInfo> info = new ArrayList<ProductInfo>();
		if (listproduct != null) {
			for (Product record : listproduct) {
				// info.add(new ProductInfo(record.getProductID(), record
				// .getProductName()));
			}
		}
		return info;
	}

	@Override
	public List<ProductInfo> getListAllProduct() {
		List<Product> listproduct = productDao.getListAllProduct();
		List<ProductInfo> info = new ArrayList<ProductInfo>();
		if (listproduct != null) {
			// System.out.println("------------------------------");
			for (Product record : listproduct) {
				// System.out.println(record.getProductCode());
				// info.add(new ProductInfo(record.getProductID(), record
				// .getProductName()));
			}
			// System.out.println("------------------------------");
		}
		return info;
	}

	@Override
	public void addProduct(ProductInfo info) {
		productDao.addProduct(info);
	}

	@Override
	public void addBrand(String brandName, String website) {
		productDao.addBrand(brandName, website);
	}

	@Override
	public List<BrandInfo> getListBrand() {
		List<Brand> listBrand = productDao.getListBrand();
		List<BrandInfo> brandInfo = new ArrayList<BrandInfo>();
		if (listBrand != null) {
			for (Brand record : listBrand) {
				brandInfo.add(new BrandInfo(record.getBrandID(), record
						.getBrandName(), record.getWebsite()));
			}
			return brandInfo;
		} else
			return null;
	}

	@Override
	public boolean checkBrandName(String brandName) {
		if (productDao.checkBrand(brandName) == true) {
			return true;
		} else
			return false;
	}

	@Override
	public BrandInfo getBrandInfo(int brandID) {
		if (productDao.getBrand(brandID) != null) {
			Brand brand = productDao.getBrand(brandID);
			BrandInfo brandInfo = new BrandInfo(brand.getBrandID(),
					brand.getBrandName(), brand.getWebsite());
			return brandInfo;
		} else
			return null;
	}

	@Override
	public void updateBrand(int brandID, String brandName, String website) {
		productDao.updateBrand(brandID, brandName, website);
	}

	@Override
	public void insertCategories(Categories cat) {
		productDao.insertCategories(cat);
	}

	public Product_Dao getProductDao() {
		return productDao;
	}

	public void setProductDao(Product_Dao productDao) {
		this.productDao = productDao;
	}

	@Override
	public Categories getCategories(Integer catID) {
		Categories cat = productDao.getCategories(catID);
		return cat;
	}

	@Override
	public boolean checkCategories(String catName) {
		if (!productDao.checkCat(catName)) {
			return false;
		} else
			return true;
	}

	@Override
	public void updateCategories(Categories info) {
		productDao.updateCategories(info);
	}

	@Override
	public boolean checkProduct(String productName) {
		if (productDao.checkProduct(productName)) {
			return true;
		} else
			return false;
	}

	@Override
	public List<ProductInfo> getTop12NewProduct() {
		List<ProductInfo> listProduct = productDao.getTop12NewProduct();
		List<ProductInfo> list = new ArrayList<ProductInfo>();
		for (ProductInfo record : listProduct) {
			ProductImageInfo imageInfo = productDao.getImageOfProduct(Integer.parseInt(record.getProductID()), 0);
			record.setListLinkImg(imageInfo.getListImgLink());
			record.setMainImgLink(imageInfo.getMainImgLink());
			list.add(record);
		}
		return list;
	}

	@Override
	public List<ProductInfo> getAllProduct(Integer sortType) {
		List<ProductInfo> listProductInfo = productDao.getAllProduct(sortType);
		List<ProductInfo> list = new ArrayList<ProductInfo>();
		for (ProductInfo record : listProductInfo) {
			ProductImageInfo imageInfo = productDao.getImageOfProduct(Integer.parseInt(record.getProductID()), 0);
			Integer agentSellingCount = 0;
			List<AgentProductInfo> listAgentProduct = shopDao.getAgentsSellingProduct(Integer.parseInt(record.getProductID()));
			if(!listAgentProduct.isEmpty()){
				for(AgentProductInfo record1 :listAgentProduct){
					agentSellingCount += 1;
				}
			}
			record.setFavorite(productDao.getFavoriteCount(Integer.parseInt(record.getProductID())));
			record.setAgentSellingCount(agentSellingCount);
			record.setListLinkImg(imageInfo.getListImgLink());
			record.setMainImgLink(imageInfo.getMainImgLink());
			list.add(record);
		}
		return list;
	}

	@Override
	public List<ProductInfo> getProductByCat(Integer catID) {
		List<ProductInfo> listProduct = productDao.getProductByCat(catID);
		List<ProductInfo> list = new ArrayList<ProductInfo>();
		for (ProductInfo record : listProduct) {
			ProductImageInfo imageInfo = productDao.getImageOfProduct(
					Integer.parseInt(record.getProductID()), 0);
			record.setListLinkImg(imageInfo.getListImgLink());
			record.setMainImgLink(imageInfo.getMainImgLink());
			list.add(record);
		}
		return list;
	}

	@Override
	public ProductInfo getProductInfo(Integer ProductID, Integer imgType) {
		ProductInfo productInfo = productDao.getProductInfo(ProductID);
		ProductImageInfo imageInfo = productDao.getImageOfProduct(Integer.parseInt(productInfo.getProductID()), imgType);
		productInfo.setMainImgLink(imageInfo.getMainImgLink());
		productInfo.setListLinkImg(imageInfo.getListImgLink());
		return productInfo;
	}

	@Override
	public void editProductDetail(ProductInfo info) {
		productDao.editProductDetail(info);
	}

	@Override
	public boolean checkCurrentProductName(String productName, String productID) {
		if (productDao.checkCurrentProductName(productName, productID)) {
			return true;
		}
		return false;
	}

	@Override
	public void deleteImage(String linkImg, String productID) {
		productDao.deleteImage(linkImg, productID);

	}

	@Override
	public boolean checkMainImg(String productID, String imgType) {
		if (productDao.checkMainImg(productID, imgType)) {
			return true;
		}
		return false;
	}

	@Override
	public void setMainImg(String productID, String imgType) {
		productDao.setMainImg(productID, imgType);

	}

	@Override
	public void addNewImg(List<Product_Image> listProductImg) {
		productDao.addNewImg(listProductImg);

	}

	@Override
	public List<ProductInfo> fullTextSearchResult(String searchText) {
		List<ProductInfo> listResult = new ArrayList<ProductInfo>();
		String text = BlankRemover.trim(searchText);
		String[]tokens = text.split("-|\\.|\\ |\\'|\\=|\\+|\\_|\\/|\\?");
		String temp = "";
		for(String record : tokens ){
			temp+= "*" + record + "*" + " ";
		}
		List<ProductInfo> listProduct = productDao.fullTextSearchResult(temp);
		for(ProductInfo record : listProduct){
			ProductInfo info = productDao.getProductInfo(Integer.parseInt(record.getProductID()));
			ProductImageInfo imageInfo = productDao.getImageOfProduct(Integer.parseInt(record.getProductID()), 0);
			info.setMainImgLink(imageInfo.getMainImgLink());
			info.setListLinkImg(imageInfo.getListImgLink());
			listResult.add(info);
		}
		return listResult;
	}

	@Override
	public List<ProductInfo> exactlySearchResult(String searchText) {
		List<ProductInfo> listResult = new ArrayList<ProductInfo>();
		String text = BlankRemover.trim(searchText);
		String[]tokens = text.split("-|\\.|\\ |\\'|\\=|\\+|\\_|\\/|\\?");
		String temp = "";
		for(String record : tokens ){
			temp+= "%" + record + "% ";
		}
		String search = BlankRemover.trim(temp);
		List<ProductInfo> listProduct = productDao.exactlySearchResult(search);
		if(!listProduct.isEmpty()){
			for(ProductInfo record : listProduct){
				ProductInfo info = productDao.getProductInfo(Integer.parseInt(record.getProductID()));
				ProductImageInfo imageInfo = productDao.getImageOfProduct(Integer.parseInt(record.getProductID()), 0);
				info.setMainImgLink(imageInfo.getMainImgLink());
				info.setListLinkImg(imageInfo.getListImgLink());
				listResult.add(info);
			}
		}
		return listResult;
	}

	@Override
	public void addMainCategories(MainCategories main) {
		productDao.addMainCategories(main);
		
	}

	@Override
	public boolean checkMainCategories(MainCategories main) {
		if(productDao.checkMainCategories(main)){
			return true;
		}
		return false;
	}

	@Override
	public List<MainCategories> getAllMainCategories() {
		List<MainCategories> listCat = productDao.getAllMainCategories();
		return listCat;
	}

	@Override
	public MainCategories viewMainCategories(Integer mainCatID) {
		MainCategories cat = productDao.viewMainCategories(mainCatID);
		return cat;
	}

	@Override
	public void editMainCategories(MainCategories mainCategories) {
		productDao.editMainCategories(mainCategories);
		
	}

	@Override
	public boolean checkCurrentCatName(Categories cat) {
		if(productDao.checkCurrentCatName(cat)){
			return true;
		}
		return false;
	}

	@Override
	public List<Categories> listAllCategories() {
		List<Categories> listCat = productDao.listAllCategories();
		return listCat;
	}

	@Override
	public String getAutoCompleteList() {
//		String result = "[";
		String result = "";
		if(productDao.getAllMainCategories() != null){
			List<MainCategories> listMainCategories = productDao.getAllMainCategories();
			for(MainCategories record: listMainCategories){
				if(record.equals(listMainCategories.get(listMainCategories.size()-1))){
					result += '"' + record.getMainCatName()+ '"';
				}else {
					result += '"' + record.getMainCatName() + '"' + ", ";
				}
			}
			result += ", ";
		}
		if(productDao.listAllCategories() != null){
			List<Categories> listCategories = productDao.listAllCategories();
			for(Categories record: listCategories){
				if(record.equals(listCategories.get(listCategories.size()-1))){
					result += '"' + record.getCatName()+ '"';
				}else {
					result += '"' + record.getCatName() + '"' + ", ";
				}
			}
			result += ", ";
		}
		if(productDao.getListBrand() != null){
			List<Brand> listBrand = productDao.getListBrand();
			for(Brand record: listBrand){
				if(record.equals(listBrand.get(listBrand.size()-1))){
					result += '"' + record.getBrandName()+ '"';
				}else {
					result += '"' + record.getBrandName() + '"' + ", ";
				}
			}
			result += ", ";
		}
		if(productDao.getAllProduct(1)!= null){
			List<ProductInfo> listProduct = productDao.getAllProduct(1);
			for(ProductInfo record: listProduct){
				if(record.equals(listProduct.get(listProduct.size()-1))){
					result += '"' + record.getCatName() + " " + record.getBrandName() + " " + record.getProductName()+ '"' + ", ";
					result += '"' + record.getBrandName() + " " + record.getProductName()+ '"' + ", ";
//					result += '"' + record.getProductName()+ '"';
				}else {
					result += '"' + record.getCatName() + " " + record.getBrandName() + " " + record.getProductName()+ '"' + ", ";
					result += '"' + record.getBrandName() + " " + record.getProductName()+ '"' + ", ";
//					result += '"' + record.getProductName() + '"' + ", ";
				}
			}
		}
//		result += "]";
		return result;
	}

	@Override
	public List<ProductInfo> list12MostFavoriteProduct() {
		List<Integer> listProductID = productDao.get12MostFavoriteProductID();
		List<ProductInfo> listProductInfo = new ArrayList<ProductInfo>();
		for(Integer record : listProductID){
			ProductInfo productInfo = productDao.getProductInfo(record);
			ProductImageInfo imageInfo = productDao.getImageOfProduct(record, 0);
			productInfo.setListLinkImg(imageInfo.getListImgLink());
			productInfo.setMainImgLink(imageInfo.getMainImgLink());
			listProductInfo.add(productInfo);
		}
		return listProductInfo;
	}
}
